package com.example.qxfw.qxxx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.*;
import com.example.qxfw.qxxx.service.SumDataCommonService;
import com.example.qxfw.ybzz.service.MsgMediumSmallScaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.*;

@Api(tags = "气象信息-历史资料")
@RestController
@CrossOrigin
@RequestMapping("/lszl")
public class LszlController {

    @Autowired
    private MsgMediumSmallScaleService msgMediumSmallScaleService;

    @Autowired
    private SumDataCommonService sumDataCommonService;

    //一些增删改查的基础接口，目前不用
    /*
    @ApiOperation("统计日数据")
    @GetMapping("/getMsgMeediumSmallScale")
    public SysResult getMsgMeediumSmallScale(String type, String en, String start, String end) {
        List<MsgMediumSmallScale> msgMediumSmallScales = msgMediumSmallScaleService.getList2(type, en, start, end);
        return SysResult.success(msgMediumSmallScales);
    }

    @ApiOperation("统计侯数据")
    @GetMapping("/getSumHouData")
    public SysResult getSumHouData(String type, String tableName, String cn, String start, String end) {
        List<SumRainHouData> houData = sumDataCommonService.getHouData(type, tableName, cn, start, end);
        return SysResult.success(houData);
    }

    @ApiOperation("统计旬数据")
    @GetMapping("/getSumXunData")
    public SysResult getSumXunData(String type, String tableName, String cn, String start, String end) {
        List<SumRainTdData> xunData = sumDataCommonService.getXunData(type, tableName, cn, start, end);
        return SysResult.success(xunData);
    }

    @ApiOperation("统计月数据")
    @GetMapping("/getSumMonthData")
    public SysResult getSumMonthData(String type, String tableName, String cn, String start, String end) {
        List<SumRainTdData> monthData = sumDataCommonService.getMonthData(type, tableName, cn, start, end);
        return SysResult.success(monthData);
    }

    @ApiOperation("统计年数据")
    @GetMapping("/getSumYearData")
    public SysResult getSumYearData(String type, String tableName, String cn, String start, String end) {
        List<SumRainYearData> yearData = sumDataCommonService.getYearData(type, tableName, cn, start, end);
        return SysResult.success(yearData);
    }

    @ApiOperation("删除日数据")
    @GetMapping("/deleteDayData")
    public SysResult deleteDayData(String tableName, String year, String month, String day, String stationId) {
        Integer rows = sumDataCommonService.deleteDayData(tableName, year, month, day, stationId);
        return SysResult.success(rows);
    }

    @ApiOperation("插入日数据")
    @PostMapping("/saveDayData")
    public SysResult saveDayData(String tableName, SumRainDayData data) {
        Integer rows = sumDataCommonService.saveDayData(tableName, data);
        return SysResult.success(rows);
    }

    @ApiOperation("删除侯数据")
    @GetMapping("/deleteHouData")
    public SysResult deleteHouData(String tableName, String year, String month, String hou, String stationId) {
        Integer rows = sumDataCommonService.deleteHouData(tableName, year, month, hou, stationId);
        return SysResult.success(rows);
    }

    @ApiOperation("添加侯数据")
    @PostMapping("/saveHouData")
    public SysResult saveHouData(String tableName, SumRainHouData sumRainHouData) {
        Integer rows = sumDataCommonService.saveHouData(tableName, sumRainHouData);
        return SysResult.success(rows);
    }

    @ApiOperation("删除旬数据")
    @GetMapping("/deleteXunData")
    public SysResult deleteXunData(String tableName, String year, String month, String td, String stationId) {
        Integer rows = sumDataCommonService.deleteXunData(tableName, year, month, td, stationId);
        return SysResult.success(rows);
    }

    @ApiOperation("添加旬数据")
    @PostMapping("/saveXunData")
    public SysResult saveXunData(String tableName, SumRainTdData sumRainTdData) {
        Integer rows = sumDataCommonService.saveXunData(tableName, sumRainTdData);
        return SysResult.success(rows);
    }

    @ApiOperation("删除月数据")
    @GetMapping("/deleteMonthData")
    public SysResult deleteMonthData(String tableName, String year, String month, String stationId) {
        Integer rows = sumDataCommonService.deleteMonthData(tableName, year, month, stationId);
        return SysResult.success(rows);
    }

    @ApiOperation("添加月数据")
    @PostMapping("/saveMonthData")
    public SysResult saveMonthData(String tableName, SumRainMonthData sumRainMonthData) {
        Integer rows = sumDataCommonService.saveMonthData(tableName, sumRainMonthData);
        return SysResult.success(rows);
    }

    @ApiOperation("删除年数据")
    @GetMapping("/deleteYearData")
    public SysResult deleteYearData(String tableName, String year, String stationId) {
        Integer rows = sumDataCommonService.deleteYearData(tableName, year, stationId);
        return SysResult.success(rows);
    }

    @ApiOperation("添加年数据")
    @PostMapping("/saveYearData")
    public SysResult saveYearData(String tableName, SumRainYearData sumRainYearData) {
        Integer rows = sumDataCommonService.saveYearData(tableName, sumRainYearData);
        return SysResult.success(rows);
    }

    @ApiOperation("获取年平均数据")
    @GetMapping("/getYearAvg")
    public SysResult getYearAvg(String tableName, String name, String y1, String y2, String stationName) {
        List<SumRainYearData> yearAvg = sumDataCommonService.getYearAvg(tableName, name, y1, y2, stationName);
        return SysResult.success(yearAvg);
    }

    @ApiOperation("获取年数据")
    @GetMapping("/getYear1")
    public SysResult getYear1(String tableName, String sumindicatorname, String stationname, String y1, String y2) {
        List<SumRainYearData> year1 = sumDataCommonService.getYear1(tableName, sumindicatorname, stationname, y1, y2);
        return SysResult.success(year1);
    }

    @ApiOperation("获取年数据")
    @GetMapping("/getYear2")
    public SysResult getYear2(String tableName, String sumindicatorname, String stationname) {
        List<SumRainYearData> year2 = sumDataCommonService.getYear2(tableName, sumindicatorname, stationname);
        return SysResult.success(year2);
    }

    @ApiOperation("获取月平均数据")
    @GetMapping("/getMonthAvg")
    public SysResult getMonthAvg(String tableName, String sumindicatorname, String y1, String y2, String stationname) {
        List<SumRainMonthData> monthAvg = sumDataCommonService.getMonthAvg(tableName, sumindicatorname, y1, y2, stationname);
        return SysResult.success(monthAvg);
    }

    @ApiOperation("获取月数据")
    @GetMapping("/getMonth")
    public SysResult getMonth(String tableName, String sumindicatorname, String stationname, String y1, String y2) {
        List<SumRainMonthData> month = sumDataCommonService.getMonth(tableName, sumindicatorname, stationname, y1, y2);
        return SysResult.success(month);
    }

    @ApiOperation("获取旬平均数据")
    @GetMapping("/getXunAvg")
    public SysResult getXunAvg(String tableName, String sumindicatorname, String y1, String y2, String stationname) {
        List<SumRainTdData> xunAvg = sumDataCommonService.getXunAvg(tableName, sumindicatorname, y1, y2, stationname);
        return SysResult.success(xunAvg);
    }

    @ApiOperation("获取旬数据")
    @GetMapping("/getXun")
    public SysResult getXun(String tableName, String sumindicatorname, String stationname, String y1, String y2) {
        List<SumRainTdData> xun = sumDataCommonService.getXun(tableName, sumindicatorname, stationname, y1, y2);
        return SysResult.success(xun);
    }

    @ApiOperation("获取侯平均数据")
    @GetMapping("/getHouAvg")
    public SysResult getHouAvg(String tableName, String sumindicatorname, String y1, String y2, String stationname) {
        List<SumRainHouData> houAvg = sumDataCommonService.getHouAvg(tableName, sumindicatorname, y1, y2, stationname);
        return SysResult.success(houAvg);
    }

    @ApiOperation("获取侯数据")
    @GetMapping("/getHou")
    public SysResult getHou(String tableName, String sumindicatorname, String stationname, String y1, String y2) {
        List<SumRainHouData> hou = sumDataCommonService.getHou(tableName, sumindicatorname, stationname, y1, y2);
        return SysResult.success(hou);
    }

    @ApiOperation("累年各月逐旬平均降雨日数")
    @GetMapping("/getXunDay")
    public SysResult getXunDay(String tableName, String sumindicatorname, String stationname, String y1, String y2) {
        List<SumRainDayData> xunDay = sumDataCommonService.getXunDay(tableName, sumindicatorname, stationname, y1, y2);
        return SysResult.success(xunDay);
    }

    @ApiOperation("获取汛期数据")
    @GetMapping("/getHighWater")
    public SysResult getHighWater(String year, String months) {
        List<SumRainMonthData> highWater = sumDataCommonService.getHighWater(year, months);
        return SysResult.success(highWater);
    }

    @ApiOperation("获取汛期平均")
    @GetMapping("/getHighWaterAvg")
    public SysResult getHighWaterAvg(String year) {
        List<SumRainMonthData> highWaterAvg = sumDataCommonService.getHighWaterAvg(year);
        return SysResult.success(highWaterAvg);
    }
    */


    /******************************************降水****************************************************/
    /***
     * 历年雨量 、年雨量距平
     * @param start
     * @param end
     * @return
     */
    @ApiOperation("降水-年降水--历年雨量、年雨量距平||平均气温-年平均气温--历年逐年平均气温||日照-历年日照时数、 距平||气压-逐年年平均气压、 距平")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 平均气压 日照时数", dataType = "String", required = true)
    })
    @GetMapping("/getYearJpData")
    public SysResult getYearJpData(@RequestParam(value = "start", required = true) String start,
                                   @RequestParam(value = "end", required = true) String end,
                                   @RequestParam(value = "stationName", required = true) String stationName,
                                   @RequestParam(value = "element", required = true) String element) {
        String tableName = "sum_rain_year_data";
        if (element.equals("总雨量")) {
            tableName = "sum_rain_year_data";
        } else if (element.equals("平均气温")) {
            tableName = "sum_temp_year_data";
        } else if (element.equals("平均气压")) {
            tableName = "sum_press_year_data";
        } else if (element.equals("日照时数")) {
            tableName = "sum_sun_year_data";
        }
        List<SumRainYearData> list = sumDataCommonService.getYearAvg(tableName, element, start, end, stationName);
        double avg = 0;
        if (list.size() > 0) {
            avg = list.get(0) == null ? 0 : list.get(0).getSumvalue();
        }
        list = sumDataCommonService.getYear1(tableName, element, stationName, start, end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        String name = element;
        DecimalFormat df = new DecimalFormat("#.##");
        //求距平均率
        for (SumRainYearData item : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("年", item.getYear());
            map.put("年" + name, item.getSumvalue());
            map.put("年均" + name, df.format(avg));
            map.put("年" + name + "距平", df.format(item.getSumvalue() - avg));
            map.put("距平百分率", Math.round((item.getSumvalue() - avg) / avg * 100));
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    @ApiOperation("降水-年降水--历年雨日、各级降水日数")
    @GetMapping("/getYearRainDayData")
    public SysResult getYearRainDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> list = sumDataCommonService.getDay(tableName, "总雨量", stationName, start, end);
        int[] rainDays = new int[5];
        List<Map<String, Object>> maplist = new ArrayList<>();
        if (list.size() > 0) {
            int year = list.get(0).getYear();
            for (SumRainDayData item : list) {
                if (item.getYear() != year) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("年", year);
                    map.put("雨日", rainDays[0]);
                    map.put("小雨日数", rainDays[1]);
                    map.put("中雨日数", rainDays[2]);
                    map.put("大雨日数", rainDays[3]);
                    map.put("≥暴雨日数", rainDays[4]);
                    maplist.add(map);
                    year = item.getYear();//换行
                    rainDays = new int[5];
                }
                double rain = item.getSumvalue();
                if (rain >= 0.1) {
                    rainDays[0]++;
                    if (rain >= 0.1 && rain < 10) {
                        rainDays[1]++;
                    } else if (rain >= 10 && rain < 25) {
                        rainDays[2]++;
                    } else if (rain >= 25 && rain < 50) {
                        rainDays[3]++;
                    } else if (rain >= 50) {
                        rainDays[4]++;
                    }
                }
            }
            //添加最后数据
            Map<String, Object> map = new HashMap<>();
            map.put("年", list.get(list.size() - 1).getYear());
            map.put("雨日", rainDays[0]);
            map.put("小雨日数", rainDays[1]);
            map.put("中雨日数", rainDays[2]);
            map.put("大雨日数", rainDays[3]);
            map.put("≥暴雨日数", rainDays[4]);
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    /**
     * @param start
     * @param end
     * @param stationName
     * @param element
     * @param name        ("总雨量", "降水",“雨量");
     *                    ("最高气温", "气温",“最高气温");
     *                    (“最低气温", "气温",“极端最低气温");
     * @return
     */
    @ApiOperation("降水-月降水--月雨量---历年逐月降雨量||气温-最高气温--历年各月最高气温||气温-最低气温--历年各月最低气温")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 最高气温 最低气温", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 最高气温 极端最低气温", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthRainData")
    public SysResult getYearMonthRainData(String start, String end, String stationName, String element, String name) {
        String yeartableName = "sum_rain_year_data";
        String monthtableName = "sum_rain_month_data";
        if (element.contains("温")) {
            yeartableName = "sum_temp_year_data";
            monthtableName = "sum_temp_month_data";
        }
        List<SumRainYearData> yearlist = sumDataCommonService.getYear1(yeartableName, element, stationName, start, end);
        List<SumRainMonthData> monthlist = sumDataCommonService.getMonth(monthtableName, element, stationName, start, end);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String year = "0";
        for (SumRainMonthData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            int x = item == null ? 0 : item.getMonth();
            //年值变化，换行
            if (String.valueOf(item.getYear()) != year) {
                map.put("年", String.valueOf(item.getYear()));
                yearlist.forEach(t -> {
                    if (t.getYear() == item.getYear()) {
                        map.put("年" + name, t.getSumvalue());
                    }
                });
            }
            year = String.valueOf(item.getYear());//获取年份
            map.put(x + "月", item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-月降水--月雨量---历年月雨量距平||平均气温-月平均气温--历年逐月平均气温、距平||湿度-历年逐月平均相对湿度、距平")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 最小相对湿度", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthJpData")
    public SysResult getYearMonthJpData(String start, String end, String stationName, String element) {
        String tableName = "sum_rain_month_data";
        if (element.equals("总雨量")) {
            tableName = "sum_rain_month_data";
        } else if (element.equals("平均气温")) {
            tableName = "sum_temp_month_data";
        } else if (element.equals("最小相对湿度")) {
            tableName = "sum_humidity_month_data";
        }
        List<SumRainMonthData> list = sumDataCommonService.getMonthAvg(tableName, element, start, end, stationName);
        double avg = 0;
        if (list.size() > 0) {
            avg = list.get(0) == null ? 0 : list.get(0).getSumvalue();
        }
        list = sumDataCommonService.getMonth(tableName, element, stationName, start, end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        String name = element;
        DecimalFormat df = new DecimalFormat("#.##");
        //求距平均率
        for (SumRainMonthData item : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("年", item.getYear());
            map.put("月", item.getMonth());
            map.put("月" + name, item.getSumvalue());
            map.put("月均" + name, df.format(avg));
            map.put("月" + name + "距平", df.format(item.getSumvalue() - avg));
            map.put("距平百分率", Math.round((item.getSumvalue() - avg) / avg * 100));
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    /**
     * @param start
     * @param end
     * @param stationName
     * @param element     ("总雨量", "降水"。"降雨量"):
     *                    ("平均气温","气温","气温");
     *                    ("最高气温","气温"，极端最高气温);
     *                    ("最低气温"，"气温"，"极端最低气温");
     * @param name
     * @return
     */
    @ApiOperation("降水-月降水--月雨量---累年逐月平均降雨||气温-月平均气温--累年月平均气温" +
            "||气温-月最高气温--累年各月极端最高气温||||气温-月最低气温--累年各月极端最低气温")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 最高气温 最低气温", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 气温 极端最高气温 极端最低气温", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthAvgData")
    public SysResult getYearMonthAvgData(String start, String end, String stationName, String element, String name) {
        String tableName = "sum_rain_month_data";
        if (element.contains("雨")) {
            tableName = "sum_rain_month_data";
        } else if (element.contains("温")) {
            tableName = "sum_temp_month_data";
        }
        List<SumRainMonthData> list = sumDataCommonService.getMonthAvg(tableName, element, start, end, stationName);
        List<Map<String, Object>> maplist = new ArrayList<>();
        if (name.contains("极端")) name = "月" + name;
        else name = "月平均" + name;
        DecimalFormat df = new DecimalFormat("#.##");
        //求距平均率
        for (SumRainMonthData item : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("月", item.getMonth());
            map.put(name, df.format(item.getSumvalue()));
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-月降水--月雨日--历年逐月降水日数")
    @GetMapping("/getMonthRainDayData")
    public SysResult getMonthRainDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> list = sumDataCommonService.getDay(tableName, "总雨量", stationName, start, end);
        int[] rainDays = new int[5];
        List<Map<String, Object>> maplist = new ArrayList<>();
        if (list.size() > 0) {
            int month = list.get(0).getMonth();
            int year = list.get(0).getYear();
            for (SumRainDayData item : list) {
                if (item.getMonth() != month) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("年", year);
                    map.put("月", month);
                    map.put("雨日", rainDays[0]);
                    map.put("小雨日数", rainDays[1]);
                    map.put("中雨日数", rainDays[2]);
                    map.put("大雨日数", rainDays[3]);
                    map.put("≥暴雨日数", rainDays[4]);
                    maplist.add(map);
                    month = item.getMonth();//换行
                    year = item.getYear();//换行
                    rainDays = new int[5];
                }
                double rain = item.getSumvalue();
                if (rain >= 0.1) {
                    rainDays[0]++;
                    if (rain >= 0.1 && rain < 10) {
                        rainDays[1]++;
                    } else if (rain >= 10 && rain < 25) {
                        rainDays[2]++;
                    } else if (rain >= 25 && rain < 50) {
                        rainDays[3]++;
                    } else if (rain >= 50) {
                        rainDays[4]++;
                    }
                }
            }
            //添加最后数据
            Map<String, Object> map = new HashMap<>();
            map.put("年", list.get(list.size() - 1).getYear());
            map.put("月", list.get(list.size() - 1).getMonth());
            map.put("雨日", rainDays[0]);
            map.put("小雨日数", rainDays[1]);
            map.put("中雨日数", rainDays[2]);
            map.put("大雨日数", rainDays[3]);
            map.put("≥暴雨日数", rainDays[4]);
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-月降水--月雨日--累年逐月各级降水平均日数")
    @GetMapping("/getYearMonthRainAvgDayData")
    public SysResult getYearMonthRainAvgDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> list = sumDataCommonService.getDay(tableName, "总雨量", stationName, start, end);
        int[] rainDays = new int[5];
        DecimalFormat df = new DecimalFormat("#.##");
        List<Map<String, Object>> maplist = new ArrayList<>();
        if (list.size() > 0) {
            int month = list.get(0).getMonth();
            int year = list.get(0).getYear();
            int count = 1;

            for (int i = 0; i < list.size(); i++) {
                SumRainDayData item = list.get(i);
                double rain = item.getSumvalue();

                if (item.getMonth() != month || i == list.size() - 1)//切换
                {
                    Map<String, Object> map = new HashMap<>();
                    map.put("月", month);
                    map.put("雨日", df.format(rainDays[0] / count));
                    map.put("小雨日数", df.format(rainDays[1] / count));
                    map.put("中雨日数", df.format(rainDays[2] / count));
                    map.put("大雨日数", df.format(rainDays[3] / count));
                    map.put("≥暴雨日数", df.format(rainDays[4] / count));
                    maplist.add(map);
                    month = item.getMonth();//换行
                    rainDays = new int[5];
                    count = 1;
                    year = item.getYear();
                }
                if (rain >= 0.1) {
                    rainDays[0]++;
                    if (rain >= 0.1 && rain < 10) {
                        rainDays[1]++;
                    } else if (rain >= 10 && rain < 25) {
                        rainDays[2]++;
                    } else if (rain >= 25 && rain < 50) {
                        rainDays[3]++;
                    } else if (rain >= 50) {
                        rainDays[4]++;
                    }
                }
                if (item.getYear() != year || i == list.size() - 1) {
                    count++;
                    year = item.getYear();
                }
            }
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-月降水--月雨日--历年各月降雨日数(≥0.1mm)")
    @GetMapping("/getYearMonthRainDayData")
    public SysResult getYearMonthRainDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> list = sumDataCommonService.getDayCount(tableName, "总雨量", stationName, start, end);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String year = "0";
        int month = 1;
        String name = "总雨量";
        for (SumRainDayData item : list) {
            Map<String, Object> map = new HashMap<>();
            int m = item == null ? 0 : item.getMonth();
            if (String.valueOf(item.getYear()) != year) {
                map.put("年", String.valueOf(item.getYear()));
                month = 1;
            }
            while (m - month > 0) {
                map.put(month + "月", 0);
                month++;
            }
            map.put(m + "月", String.valueOf(item.getDay()));
            year = String.valueOf(item.getYear());//获取年份
            month++;
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    /**
     * @param start
     * @param end
     * @param stationName
     * @param element
     * @param name        ("总雨量"，“降水"，"雨量");
     *                    n("平均气温", "气温", "平均气温");
     *                    n("最高气温", "气温",“平均最高");
     *                    n("最低气温"，"气温"，"平均最低");
     *                    n("日照时数"，"日照", "日照时数");
     *                    n("平均气压", "气压", "气压");
     * @return
     */
    @ApiOperation("降水-旬降水--旬雨量---历年各月逐旬降雨量||气温-平均气温--旬平均气温---历年逐旬平均气温" +
            "||气温-最高气温--历年各月逐旬平均最高气温||气温-最低气温--历年各月逐旬平均最低气温" +
            "||日照-历年各月逐旬日照时数||气压-历年逐月、旬平均气压")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 最高气温 最低气温 日照时数 平均气压", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 平均气温 平均最高 平均最低 日照时数 气压", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthXunRainData")
    public SysResult getYearMonthXunRainData(String start, String end, String stationName, String element, String name) {
        String monthtableName = "sum_rain_month_data";
        String tdtableName = "sum_rain_td_data";
        if (element.contains("雨")) {
            monthtableName = "sum_rain_month_data";
            tdtableName = "sum_rain_td_data";
        } else if (element.contains("温")) {
            monthtableName = "sum_temp_month_data";
            tdtableName = "sum_temp_td_data";
        } else if (element.contains("压")) {
            monthtableName = "sum_press_month_data";
            tdtableName = "sum_press_td_data";
        } else if (element.contains("日照")) {
            monthtableName = "sum_sun_month_data";
            tdtableName = "sum_sun_td_data";
        }
        List<SumRainMonthData> monthlist = sumDataCommonService.getMonth(monthtableName, element, stationName, start, end);
        List<SumRainTdData> tdlist = sumDataCommonService.getXun(tdtableName, element, stationName, start, end);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String month = "0";
        for (SumRainTdData item : tdlist) {
            Map<String, Object> map = new HashMap<>();
            int x = item == null ? 0 : item.getTd();
            //年值变化，换行
            if (String.valueOf(item.getMonth()) != month) {
                map.put("年", String.valueOf(item.getYear()));
                map.put("月", String.valueOf(item.getMonth()));
                monthlist.forEach(t -> {
                    if (t.getMonth() == item.getMonth()) {
                        map.put("月" + name, t.getSumvalue());
                    }
                });
            }
            month = String.valueOf(item.getMonth());//获取年份
            if (x == 1)
                map.put("上旬" + name, item.getSumvalue());
            else if (x == 2)
                map.put("中旬" + name, item.getSumvalue());
            else if (x == 3)
                map.put("下旬" + name, item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    /**
     * @param start
     * @param end
     * @param stationName
     * @param element     ("总雨量"，"降水", "雨量");
     *                    ("平均气温"，"气温", "平均气温");
     *                    ("最高气温"，"气温"，"平均最高"):
     *                    ("最低气温"，"气温"，"平均最低");
     *                    ("日照时数", "日照"，"日照时数");
     *                    ("平均气压", "气压", "气压");
     * @param name
     * @return
     */
    @ApiOperation("降水-旬降水--旬雨量---累年各月逐旬平均降雨量||气温-平均气温--旬平均气温---累年逐旬平均气温" +
            "||日照-累年各月逐旬平均日照时数||气压-累年逐月、旬平均气压")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 最高气温 最低气温 日照时数 平均气压", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 平均气温 平均最高 平均最低 日照时数 气压", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthXunAvgRainData")
    public SysResult getYearMonthXunAvgRainData(String start, String end, String stationName, String element, String name) {
        String monthtableName = "sum_rain_month_data";
        String tdtableName = "sum_rain_td_data";
        if (element.contains("雨")) {
            monthtableName = "sum_rain_month_data";
            tdtableName = "sum_rain_td_data";
        } else if (element.contains("温")) {
            monthtableName = "sum_temp_month_data";
            tdtableName = "sum_temp_td_data";
        } else if (element.contains("压")) {
            monthtableName = "sum_press_month_data";
            tdtableName = "sum_press_td_data";
        } else if (element.contains("日照")) {
            monthtableName = "sum_sun_month_data";
            tdtableName = "sum_sun_td_data";
        }
        List<SumRainMonthData> monthlist = sumDataCommonService.getMonthAvg(monthtableName, element, start, end, stationName);
        List<SumRainTdData> tdlist = sumDataCommonService.getXunAvg(tdtableName, element, start, end, stationName);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String month = "0";
        for (SumRainTdData item : tdlist) {
            Map<String, Object> map = new HashMap<>();
            int x = item == null ? 0 : item.getTd();
            //年值变化，换行
            if (String.valueOf(item.getMonth()) != month) {
                map.put("月", String.valueOf(item.getMonth()));
                monthlist.forEach(t -> {
                    if (t.getMonth() == item.getMonth()) {
                        map.put("月平均", t.getSumvalue());
                    }
                });
            }
            month = String.valueOf(item.getMonth());//获取年份
            if (x == 1)
                map.put("上旬平均" + name, item.getSumvalue());
            else if (x == 2)
                map.put("中旬平均" + name, item.getSumvalue());
            else if (x == 3)
                map.put("下旬平均" + name, item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-旬降水--旬雨日--历年各月逐旬降雨日数")
    @GetMapping("/getMonthXunRainDayData")
    public SysResult getMonthXunRainDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> xunList = sumDataCommonService.getXunDay(tableName, "总雨量", stationName, start, end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        if (xunList.size() > 0) {
            String month = "0";
            int monRainDays = 0;//计算月降雨日数
            int xunIndex = Integer.parseInt(xunList.get(0).getStationid());
            int monIndex = xunList.get(0).getMonth();
            int year = xunList.get(0).getYear();
            for (SumRainDayData item : xunList) {
                Map<String, Object> cmmap = new HashMap<>();
                if (String.valueOf(item.getMonth()) != month) {
                    xunIndex--;//还原
                    //校验旬
                    if (xunIndex < 3 && month != "0")//旬缺失
                    {
                        while (3 - xunIndex > 0) {
                            xunIndex++;
                            if (xunIndex == 1)
                                cmmap.put("上旬雨日", 0);
                            if (xunIndex == 2)
                                cmmap.put("中旬雨日", 0);
                            if (xunIndex == 3)
                                cmmap.put("下旬雨日", 0);
                            cmmap.put("月雨日", monRainDays);
                        }
                    }
                    if (item.getYear() != year) {
                        monIndex--;
                        while (12 - monIndex > 0)//缺失月份补齐
                        {
                            monIndex++;
                            Map<String, Object> map = new HashMap<>();
                            map.put("年", item.getYear());
                            map.put("月", monIndex);
                            map.put("月雨日", 0);
                            map.put("上旬雨日", 0);
                            map.put("中旬雨日", 0);
                            map.put("下旬雨日", 0);
                            maplist.add(map);
                        }
                        monIndex = 1;//初始化
                        year = item.getYear();
                    }

                    //校验月
                    if (item.getMonth() - monIndex > 0 && month != "0")//月降水为0
                    {
                        while (item.getMonth() - monIndex > 0) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("年", item.getYear());
                            map.put("月", monIndex);
                            map.put("月雨日", 0);
                            map.put("上旬雨日", 0);
                            map.put("中旬雨日", 0);
                            map.put("下旬雨日", 0);
                            maplist.add(map);
                            monIndex++;
                        }
                    } else {
                        cmmap.put("年", item.getYear());
                        cmmap.put("月", item.getMonth());
                        month = String.valueOf(item.getMonth());
                        monIndex++;
                        xunIndex = 1;
                        monRainDays = 0;
                    }
                }

                int x = Integer.parseInt(item.getStationid());//旬
                while (x - xunIndex > 0) {
                    if (xunIndex == 1)
                        cmmap.put("上旬雨日", 0);
                    if (xunIndex == 2)
                        cmmap.put("中旬雨日", 0);
                    if (xunIndex == 3)
                        cmmap.put("下旬雨日", 0);
                    xunIndex++;
                }
                if (x == 1)
                    cmmap.put("上旬雨日", String.valueOf(item.getDay()));//旬降雨日数
                if (x == 2)
                    cmmap.put("中旬雨日", String.valueOf(item.getDay()));//旬降雨日数
                if (x == 3)
                    cmmap.put("下旬雨日", String.valueOf(item.getDay()));//旬降雨日数
                monRainDays += item.getDay();
                if ((2 + x) == cmmap.keySet().size() - 1) {
                    cmmap.put("月雨日", String.valueOf(monRainDays));//月值
                    monRainDays = 0;
                }

                maplist.add(cmmap);
                xunIndex++;
            }
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-旬降水--旬雨日--累年各月逐旬平均降雨日数")
    @GetMapping("/getYearMonthXunRainAvgDayData")
    public SysResult getYearMonthXunRainAvgDayData(String start, String end, String stationName) {
        String tableName = "sum_rain_day_data";
        List<SumRainDayData> xunList = sumDataCommonService.getXunDay(tableName, "总雨量", stationName, start, end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.##");
        if (xunList.size() > 0) {
            int month = 0;
            Map<String, Object> cmmap = null;
            double sumMonRain = 0d;//月均降水
            double sumXunRain = 0d;//旬均降水
            int xunIndex = Integer.parseInt(xunList.get(0).getStationid());//旬值，变化是统计旬平均
            int yearCount = Integer.parseInt(end) - Integer.parseInt(start) + 1;//年计数
            int year = xunList.get(0).getYear();
            for (int i = 0; i < xunList.size(); i++) {
                SumRainDayData item = xunList.get(i);
                if (item.getMonth() != month)//月份换行
                {
                    if (month != 0) {
                        sumMonRain += sumXunRain / yearCount;
                        cmmap.put("月平均雨日", df.format(sumMonRain));
                        if (xunIndex == 1)
                            cmmap.put("上旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        if (xunIndex == 2)
                            cmmap.put("中旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        if (xunIndex == 3)
                            cmmap.put("下旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        while (item.getMonth() - month > 1) {
                            month++;
                            Map<String, Object> map = new HashMap<>();
                            map.put("月", month);
                            map.put("月平均雨日", 0);
                            map.put("上旬平均雨日", 0);
                            map.put("中旬平均雨日", 0);
                            map.put("下旬平均雨日", 0);
                            maplist.add(map);
                        }
                    }
                    if (cmmap != null)
                        maplist.add(cmmap);
                    cmmap = new HashMap<>();
                    cmmap.put("月", item.getMonth());
                    month = item.getMonth();
                    sumMonRain = 0;
                    sumXunRain = 0;
                    xunIndex = 1;
                }
                //旬变化时统计旬平均
                int x = Integer.parseInt(item.getStationid());//旬号
                if (String.valueOf(xunIndex) != item.getStationid()) {
                    if ((xunIndex == 1 && sumXunRain == 0)) {
                        while (x - xunIndex > 1) {
                            xunIndex++;
                            if (xunIndex == 1)
                                cmmap.put("上旬平均雨日", 0);//旬降雨日数
                            if (xunIndex == 2)
                                cmmap.put("中旬平均雨日", 0);//旬降雨日数
                            if (xunIndex == 3)
                                cmmap.put("下旬平均雨日", 0);//旬降雨日数
                        }
                    } else {
                        sumMonRain += sumXunRain / yearCount;
                        if (xunIndex == 1)
                            cmmap.put("上旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        if (xunIndex == 2)
                            cmmap.put("中旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        if (xunIndex == 3)
                            cmmap.put("下旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                        sumXunRain = 0;
                        while (x - xunIndex > 1) {
                            xunIndex++;
                            if (xunIndex == 1)
                                cmmap.put("上旬平均雨日", 0);//旬降雨日数
                            if (xunIndex == 2)
                                cmmap.put("中旬平均雨日", 0);//旬降雨日数
                            if (xunIndex == 3)
                                cmmap.put("下旬平均雨日", 0);//旬降雨日数
                        }
                        xunIndex = Integer.parseInt(item.getStationid());//更新旬号
                    }
                }

                sumXunRain += item.getDay();

                //最后旬
                if (i == xunList.size() - 1) {
                    sumMonRain += sumXunRain / yearCount;
                    if (x == 1)
                        cmmap.put("上旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                    if (x == 2)
                        cmmap.put("中旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                    if (x == 3)
                        cmmap.put("下旬平均雨日", df.format(sumXunRain / yearCount));//旬降雨日数
                    cmmap.put("月平均雨日", df.format(sumMonRain));
                    maplist.add(cmmap);
                    //校验
                    sumMonRain = 0;
                    sumXunRain = 0;
                }
            }
        }
        return SysResult.success(maplist);
    }


    /**
     * @param start
     * @param end
     * @param stationName
     * @param element     总雨量 平均气温
     * @param name        雨量 平均气温
     * @return
     */
    @ApiOperation("降水-侯降水--历年逐月各侯降雨量||气温-平均气温--侯平均气温---历年逐侯平均气温")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 平均气温", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthHouRainData")
    public SysResult getYearMonthHouRainData(String start, String end, String stationName, String element, String name) {
        String monthtableName = "sum_rain_month_data";
        String houtableName = "sum_rain_hou_data";
        if (element.contains("雨")) {
            monthtableName = "sum_rain_month_data";
            houtableName = "sum_rain_hou_data";
        } else if (element.contains("温")) {
            monthtableName = "sum_temp_month_data";
            houtableName = "sum_temp_hou_data";
        }
        List<SumRainMonthData> monthlist = sumDataCommonService.getMonth(monthtableName, element, stationName, start, end);
        List<SumRainHouData> tdlist = sumDataCommonService.getHou(houtableName, element, stationName, start, end);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String month = "0";
        for (SumRainHouData item : tdlist) {
            Map<String, Object> map = new HashMap<>();
            int x = item == null ? 0 : item.getHou();
            //年值变化，换行
            if (String.valueOf(item.getMonth()) != month) {
                map.put("年", String.valueOf(item.getYear()));
                map.put("月", String.valueOf(item.getMonth()));
                monthlist.forEach(t -> {
                    if (t.getMonth() == item.getMonth()) {
                        map.put("月" + name, t.getSumvalue());
                    }
                });
            }
            month = String.valueOf(item.getMonth());//获取年份
            if (x == 1)
                map.put("第一侯" + name, item.getSumvalue());
            else if (x == 2)
                map.put("第二侯" + name, item.getSumvalue());
            else if (x == 3)
                map.put("第三侯" + name, item.getSumvalue());
            else if (x == 4)
                map.put("第四侯" + name, item.getSumvalue());
            else if (x == 5)
                map.put("第五侯" + name, item.getSumvalue());
            else if (x == 6)
                map.put("第六侯" + name, item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    /**
     * @param start
     * @param end
     * @param stationName
     * @param element     总雨量 平均气温 日照时数
     * @param name        雨量 平均气温 日照时数
     * @return
     */
    @ApiOperation("降水-侯降水--累年逐月各侯平均降雨量||气温-平均气温--侯平均气温---累年逐旬平均气温||日照-累年各月、侯平均日照时数")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：总雨量 平均气温 日照时数", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：雨量 平均气温 日照时数", dataType = "String", required = true)
    })
    @GetMapping("/getYearMonthHouAvgRainData")
    public SysResult getYearMonthHouAvgRainData(String start, String end, String stationName, String element, String name) {
        String monthtableName = "sum_rain_month_data";
        String houtableName = "sum_rain_hou_data";
        if (element.contains("雨")) {
            monthtableName = "sum_rain_month_data";
            houtableName = "sum_rain_hou_data";
        } else if (element.contains("温")) {
            monthtableName = "sum_temp_month_data";
            houtableName = "sum_temp_hou_data";
        } else if (element.contains("日照")) {
            monthtableName = "sum_sun_month_data";
            houtableName = "sum_sun_hou_data";
        }
        List<SumRainMonthData> monthlist = sumDataCommonService.getMonthAvg(monthtableName, element, start, end, stationName);
        List<SumRainHouData> tdlist = sumDataCommonService.getHouAvg(houtableName, element, start, end, stationName);

        List<Map<String, Object>> maplist = new ArrayList<>();
        String month = "0";
        for (SumRainHouData item : tdlist) {
            Map<String, Object> map = new HashMap<>();
            int x = item == null ? 0 : item.getHou();
            //年值变化，换行
            if (String.valueOf(item.getMonth()) != month) {
                map.put("月", String.valueOf(item.getMonth()));
                monthlist.forEach(t -> {
                    if (t.getMonth() == item.getMonth()) {
                        map.put("月平均", t.getSumvalue());
                    }
                });
            }
            month = String.valueOf(item.getMonth());//获取年份
            if (x == 1)
                map.put("第一侯" + name, item.getSumvalue());
            else if (x == 2)
                map.put("第二侯" + name, item.getSumvalue());
            else if (x == 3)
                map.put("第三侯" + name, item.getSumvalue());
            else if (x == 4)
                map.put("第四侯" + name, item.getSumvalue());
            else if (x == 5)
                map.put("第五侯" + name, item.getSumvalue());
            else if (x == 6)
                map.put("第六侯" + name, item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }


    @ApiOperation("降水-汛期降水--汛期")
    @GetMapping("/getXunqiRainData")
    public SysResult getXunqiRainData(String start,String end) {
        List<SumRainMonthData> monthlist = sumDataCommonService.getHighWaterXq(start,end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (SumRainMonthData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("站点编号", item.getStationid());
            map.put("站点名称", item.getStationname());
            map.put("累积降雨", item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    @ApiOperation("降水-汛期降水--前汛期")
    @GetMapping("/getBeforeXunqiRainData")
    public SysResult getBeforeXunqiRainData(String start,String end) {
        List<SumRainMonthData> monthlist = sumDataCommonService.getHighWaterXqBefore(start,end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (SumRainMonthData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("站点编号", item.getStationid());
            map.put("站点名称", item.getStationname());
            map.put("累积降雨", item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    @ApiOperation("降水-汛期降水--后汛期")
    @GetMapping("/getAfterXunqiRainData")
    public SysResult getAfterXunqiRainData(String start,String end) {
        List<SumRainMonthData> monthlist = sumDataCommonService.getHighWaterXqAfter(start,end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (SumRainMonthData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("站点编号", item.getStationid());
            map.put("站点名称", item.getStationname());
            map.put("累积降雨", item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    @ApiOperation("降水-汛期降水--多年平均")
    @GetMapping("/getAvgXunqiRainData")
    public SysResult getAvgXunqiRainData(String start, String end) {
        List<SumRainMonthData> monthlist = sumDataCommonService.getHighWaterXqAvg(start, end);
        List<Map<String, Object>> maplist = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.##");
        int yearCount = Integer.parseInt(end) - Integer.parseInt(start) + 1;
        for (SumRainMonthData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("站点编号", item.getStationid());
            map.put("站点名称", item.getStationname());
            map.put("累积降雨", df.format(item.getSumvalue() / yearCount));
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }
    /******************************************降水结束****************************************************/
    /******************************************气温开始****************************************************/


    /**
     * @param element     最高气温 最低气温
     * @param name        极端最高气温 极端最低气温
     * @param stationname
     * @return
     */
    @ApiOperation("气温-最高气温--历年极端最高气温||气温-最低气温--历年极端最低气温")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始年", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束年", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "element", value = "要素：最高气温 最低气温", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "别名：极端最高气温 极端最低气温", dataType = "String", required = true)
    })
    @GetMapping("/getMaxtempData")
    public SysResult getMaxtempData(String element, String name, String stationname) {
        String tableName = "sum_temp_year_data";
        List<SumRainYearData> monthlist = sumDataCommonService.getYear2(tableName, element, stationname);
        List<Map<String, Object>> maplist = new ArrayList<>();
        for (SumRainYearData item : monthlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("年", item.getYear());
            map.put("年" + name, item.getSumvalue());
            maplist.add(map);
        }
        return SysResult.success(maplist);
    }

    /******************************************气温结束****************************************************/
    /******************************************曲线列表****************************************************/

    @ApiOperation("曲线列表-曲线")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间 年[yyyy] 季度[yyyy-第几季] 月[yyyy-第几月] 旬[yyyy-第几月-第几旬] 侯[yyyy-MM-第几侯] 日[yyyy-第几月-第几日]", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束时间 年[yyyy] 季度[yyyy-第几季] 月[yyyy-第几月] 旬[yyyy-第几月-第几旬] 侯[yyyy-MM-第几侯] 日[yyyy-第几月-第几日]", dataType = "String", required = true),
            @ApiImplicitParam(name = "elementName", value = "要素中文", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "类别：年 季度 月 旬 侯 日", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称：配置文件配置 凌云", dataType = "String", required = true),
            @ApiImplicitParam(name = "val", value = "type为季度则值代表季度,为月份代表月", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "tdhou", value = "type为旬时使用,旬 1,2,3", dataType = "String", required = true),
            //@ApiImplicitParam(name = "hou", value = "type为侯时使用,侯 1,2,3,4,5,6", dataType = "String", required = true)
    })
    @GetMapping("/getHistoryDataChart")
    public SysResult getHistoryDataChart(String start, String end, String elementName, String type, String stationName,Integer val,String tdhou) {
        String enName = getEnName(elementName);
        String tableName = "";

        List<SumRainYearData> yearlist = new ArrayList<>();
        String y1 = "";
        String y2 = "";
        String m = "";
        String m1 = "";
        String m2 = "";
        switch (type) {
            case "年":
                tableName = "sum_" + enName + "_year_data";
                yearlist = sumDataCommonService.getYear1(tableName, elementName, stationName, start.substring(0, 4), end.substring(0, 4));
                break;
            case "季度":
                tableName = "sum_" + enName + "_month_data";
                if (start.split("-").length >= 2) {
                    y1 = start.split("-")[0];
                    y2 = end.split("-")[0];
                    m1 = String.valueOf((val - 1)*3+1);
//                    m1 = start.split("-")[1];
//                    m2 = end.split("-")[1];
                    m2 = String.valueOf(val*3);
                    yearlist = sumDataCommonService.getYear3Month(tableName, elementName, stationName, y1, y2, m1, m2);
                }
                break;
            case "月":
                tableName = "sum_" + enName + "_month_data";
                if (start.split("-").length >= 2) {
                    y1 = start.split("-")[0];
                    y2 = end.split("-")[0];
//                    m = start.split("-")[1];
                    yearlist = sumDataCommonService.getYearMonth(tableName, elementName, stationName, y1, y2, String.valueOf(val));
                }
                break;
            case "旬":
                tableName = "sum_" + enName + "_td_data";
                if (start.split("-").length == 3) {
                    y1 = start.split("-")[0];
                    y2 = end.split("-")[0];
//                    m = start.split("-")[1];
//                    String td = start.split("-")[2];



                    yearlist = sumDataCommonService.getYearTd(tableName, elementName, stationName, y1, y2, String.valueOf(val), tdhou);
                }
                break;
            case "侯":
                tableName = "sum_" + enName + "_hou_data";
                if (start.split("-").length == 3) {
                    y1 = start.split("-")[0];
                    y2 = end.split("-")[0];
//                    m = start.split("-")[1];
//                    String hou = start.split("-")[2];
                    yearlist = sumDataCommonService.getYearHou(tableName, elementName, stationName, y1, y2, String.valueOf(val), tdhou);
                }
                break;
            case "日":
                tableName = "sum_" + enName + "_day_data";
                if (start.split("-").length == 3) {
                    y1 = start.split("-")[0];
                    y2 = end.split("-")[0];
                    m1 = start.split("-")[1];
                    m2 = end.split("-")[1];
                    String d1 = start.split("-")[2];
                    String d2 = end.split("-")[2];
                    String optype = getOptype(elementName);
                    yearlist = sumDataCommonService.getYearDay(optype, tableName, elementName, stationName, y1, y2, m1, m2, d1, d2);
                }
                break;
        }
        Map<String, Object> map = new HashMap<>();
        if (yearlist.size() > 0) {
            int[] xlist = new int[yearlist.size()];
            double[] ylist = new double[yearlist.size()];
            for (int k = 0; k < yearlist.size(); k++) {
                xlist[k] = yearlist.get(k).getYear();
                ylist[k] = yearlist.get(k).getSumvalue();
            }
            map.put("x", xlist);
            map.put("y", ylist);
            return SysResult.success(map);
        }
        return SysResult.fail("未查询到数据");
    }


    @ApiOperation("曲线列表-时间段查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间 yyyy-MM-dd", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束时间 yyyy-MM-dd", dataType = "String", required = true),
            @ApiImplicitParam(name = "elementName", value = "要素中文", dataType = "String", required = true),
            @ApiImplicitParam(name = "stationName", value = "站点名称：配置文件配置 凌云", dataType = "String", required = true)
    })
    @GetMapping("/getHistoryDataStatistics")
    public SysResult getHistoryDataStatistics(String start, String end, String elementName, String stationName) {
        String enName = getEnName(elementName);
        String tableName = "sum_" + enName + "_day_data";

        Map<String, Object> map = new HashMap<>();
        String optype = getOptype(elementName);
        String units = getElementUnit(elementName);

        List<SumRainDayData> daylist = sumDataCommonService.getDayData1(optype, tableName, elementName, stationName, start, end);
        String content = elementName + "统计\r\n";
        for (SumRainDayData item : daylist) {
            content += item.getStationname() + ":" + item.getSumvalue() + units + "\r\n";
        }
        map.put("统计",content);
        List<SumRainDayData> daylist2 = sumDataCommonService.getDayData2(optype, tableName, elementName, stationName, start, end);
        map.put("列表",daylist2);
        return SysResult.success(map);
    }

    private String getElementUnit(String element) {
        if (element.contains("气温")) {
            element = "（℃）";
        } else if (element.contains("日照")) {
            element = "（小时）";
        } else if (element.contains("风速")) {
            element = "（m/s）";
        } else if (element.contains("气压")) {
            element = "（hPa）";
        } else if (element.contains("湿度")) {
            element = "（%）";
        } else if (element.contains("雨量")) {
            element = "（mm）";
        }

        return element;
    }
    /******************************************曲线列表****************************************************/
    /******************************************历史数据****************************************************/

    @ApiOperation("历史数据")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间 yyyy-MM-dd", dataType = "String", required = true),
            @ApiImplicitParam(name = "end", value = "结束时间 yyyy-MM-dd", dataType = "String", required = true),
            @ApiImplicitParam(name = "elementName", value = "要素中文", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "类别：年 月 旬 侯 日", dataType = "String", required = true)
    })
    @GetMapping("/getHistoryData")
    public SysResult getHistoryData(String start, String end, String elementName, String type) {
        String enName = getEnName(elementName);
        String tableName = "sum_" + enName + "_day_data";

        String en = getEn(elementName);
        String optype = getOptype(elementName);

        List<Map<String, Object>> maplist = new ArrayList<>();
        switch (type) {
            case "年":
                start = start.substring(0, 4);
                end = end.substring(0, 4);
                List<SumRainYearData> yearlist = sumDataCommonService.getYearData(optype, tableName, elementName, start, end);
                for (SumRainYearData item : yearlist) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("站号", item.getStationid());
                    map.put("站名", item.getStationname());
                    map.put("年", item.getYear());
                    map.put(en, item.getSumvalue());
                    maplist.add(map);
                }
                break;
            case "月":
                start = start.substring(0, 4) + start.substring(5, 7);
                end = end.substring(0, 4) + end.substring(5, 7);
                List<SumRainMonthData> monthlist = sumDataCommonService.getMonthData(optype, tableName, elementName, start, end);
                for (SumRainMonthData item : monthlist) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("站号", item.getStationid());
                    map.put("站名", item.getStationname());
                    map.put("年", item.getYear());
                    map.put("月", item.getMonth());
                    map.put(en, item.getSumvalue());
                    maplist.add(map);
                }
                break;
            case "旬":
                start = start.substring(0, 4) + start.substring(5, 7) + start.substring(8, 10);
                end = end.substring(0, 4) + end.substring(5, 7) + end.substring(8, 10);
                List<SumRainTdData> tdlist = sumDataCommonService.getXunData(optype, tableName, elementName, start, end);
                for (SumRainTdData item : tdlist) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("站号", item.getStationid());
                    map.put("站名", item.getStationname());
                    map.put("年", item.getYear());
                    map.put("月", item.getMonth());
                    map.put("旬", item.getTd());
                    map.put(en, item.getSumvalue());
                    maplist.add(map);
                }
                break;
            case "侯":
                start = start.substring(0, 4) + start.substring(5, 7) + start.substring(8, 10);
                end = end.substring(0, 4) + end.substring(5, 7) + end.substring(8, 10);
                List<SumRainHouData> houlist = sumDataCommonService.getHouData(optype, tableName, elementName, start, end);
                for (SumRainHouData item : houlist) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("站号", item.getStationid());
                    map.put("站名", item.getStationname());
                    map.put("年", item.getYear());
                    map.put("月", item.getMonth());
                    map.put("侯", item.getHou());
                    map.put(en, item.getSumvalue());
                    maplist.add(map);
                }
                break;
            case "日":
                List<MsgMediumSmallScale> msgMediumSmallScales = msgMediumSmallScaleService.getList2(optype, en, start, end);
                for (MsgMediumSmallScale item : msgMediumSmallScales) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("站号", item.getStationid());
                    map.put("站名", item.getStationname());
                    map.put("经度", item.getLongitude());
                    map.put("纬度", item.getLatitude());
                    map.put("日期", item.getObservtime());
                    map.put(en, item.getRain());
                    maplist.add(map);
                }
                break;
        }
        return SysResult.success(maplist);
    }

    /******************************************历史数据****************************************************/
    private String getOptype(String elementName) {
        String optype = "";
        if (elementName.contains("最高") || elementName.contains("最大") || elementName.contains("极大")) {
            optype = "MAX";
        } else if (elementName.contains("雨量") || elementName.contains("日照")) {
            optype = "SUM";
        } else if (elementName.contains("最低") || elementName.contains("最小")) {
            optype = "MIN";
        } else {
            optype = "AVG";
        }
        return optype;
    }

    private String getEn(String elementName) {
        String en = "";
        switch (elementName) {
            case "平均风速":
                en = "InstantWindV";
                break;
            case "极大风速":
                en = "HExMaxWindV";
                break;
            case "最大风速":
                en = "MaxWindV";
                break;
            case "总雨量":
                en = "Rain";
                break;
            case "平均气温":
                en = "DryBulbTemp";
                break;
            case "最高气温":
                en = "MaxTemp";
                break;
            case "最低气温":
                en = "MinTemp";
                break;
            case "相对湿度":
                en = "RelHumidity";
                break;
            case "最小相对湿度":
                en = "MinRelHumidity";
                break;
            case "平均气压":
                en = "StationPress";
                break;
            case "最高气压":
                en = "MaxStationPress";
                break;
        }
        return en;
    }

    private String getEnName(String elementName) {
        String enName = "";
        if (elementName.contains("降水") || elementName.contains("雨"))
            enName = "rain";
        if (elementName.contains("温"))
            enName = "temp";
        if (elementName.contains("风"))
            enName = "wind";
        if (elementName.contains("湿"))
            enName = "humidity";
        if (elementName.contains("压"))
            enName = "press";
        if (elementName.contains("日照"))
            enName = "sun";
        return enName;
    }

}
